home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / INTERCOM.C < prev    next >
C/C++ Source or Header  |  1990-06-20  |  10KB  |  309 lines

  1. /* -*-C-*-
  2.  
  3. $Header: intercom.c,v 9.28 90/06/20 17:41:04 GMT cph Exp $
  4.  
  5. Copyright (c) 1987, 1988, 1989, 1990 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Single-processor simulation of locking, propagating, and
  36.    communicating stuff. */
  37.  
  38. #include "scheme.h"
  39. #include "prims.h"
  40. #include "locks.h"
  41. #include "zones.h"
  42.  
  43. #ifndef COMPILE_FUTURES
  44. #include "Error: intercom.c is useless without COMPILE_FUTURES"
  45. #endif
  46.  
  47. /* (GLOBAL-INTERRUPT LEVEL WORK TEST)
  48.  
  49.    There are 4 global interrupt levels, level 0 (highest priority)
  50.    being reserved for GC.  See const.h for details of the dist-
  51.    ribution of these bits with respect to local interrupt levels.
  52.  
  53.    Force all other processors to begin executing WORK (an interrupt
  54.    handler [procedure of two arguments]) provided that TEST returns
  55.    true.  TEST is supplied to allow this primitive to be restarted if it
  56.    is unable to begin because another processor wins the race to
  57.    generate a global interrupt and makes it no longer necessary that
  58.    this processor generate one (TEST receives no arguments).  This
  59.    primitive returns the value of the call to TEST (i.e. non-#!FALSE if
  60.    the interrupt was really generated), and returns only after all other
  61.    processors have begun execution of WORK (or TEST returns false).
  62. */
  63.  
  64. DEFINE_PRIMITIVE ("GLOBAL-INTERRUPT", Prim_send_global_interrupt, 3, 3, 0)
  65. {
  66.   long Which_Level;
  67.   SCHEME_OBJECT work;
  68.   SCHEME_OBJECT test;
  69.   long Saved_Zone;
  70.   PRIMITIVE_HEADER (3);
  71.   PRIMITIVE_CANONICALIZE_CONTEXT ();
  72.   Which_Level = (arg_index_integer (1, 4));
  73.   work = (ARG_REF (2));        /* Why is this being ignored? -- CPH */
  74.   test = (ARG_REF (3));
  75.   Save_Time_Zone (Zone_Global_Int);
  76.   POP_PRIMITIVE_FRAME (3);
  77.  Will_Push (CONTINUATION_SIZE + STACK_ENV_EXTRA_SLOTS + 1);
  78.   Store_Return (RC_FINISH_GLOBAL_INT);
  79.   Store_Expression (LONG_TO_UNSIGNED_FIXNUM (Which_Level));
  80.   Save_Cont ();
  81.   STACK_PUSH (test);
  82.   STACK_PUSH (STACK_FRAME_HEADER);
  83.  Pushed ();
  84.   Restore_Time_Zone ();
  85.   PRIMITIVE_ABORT (PRIM_APPLY);
  86.   /*NOTREACHED*/
  87. }
  88.  
  89. SCHEME_OBJECT
  90. Global_Int_Part_2 (Which_Level, Do_It)
  91.      SCHEME_OBJECT Which_Level;
  92.      SCHEME_OBJECT Do_It;
  93. {
  94.   return (Do_It);
  95. }
  96.  
  97. DEFINE_PRIMITIVE ("PUT-WORK", Prim_put_work, 1, 1, 0)
  98. {
  99.   PRIMITIVE_HEADER (1);
  100.   {
  101.     SCHEME_OBJECT queue = (Get_Fixed_Obj_Slot (The_Work_Queue));
  102.     if (queue == EMPTY_LIST)
  103.       {
  104.     queue = (cons (EMPTY_LIST, EMPTY_LIST));
  105.     Set_Fixed_Obj_Slot (The_Work_Queue, queue);
  106.       }
  107.     {
  108.       SCHEME_OBJECT queue_tail = (PAIR_CDR (queue));
  109.       SCHEME_OBJECT new_entry = (cons ((ARG_REF (1)), EMPTY_LIST));
  110.       SET_PAIR_CDR (queue, new_entry);
  111.       if (queue_tail == EMPTY_LIST)
  112.     SET_PAIR_CAR (queue, new_entry);
  113.       else
  114.     SET_PAIR_CDR (queue_tail, new_entry);
  115.     }
  116.   }
  117.   PRIMITIVE_RETURN (UNSPECIFIC);
  118. }
  119.  
  120. DEFINE_PRIMITIVE ("PUT-WORK-IN-FRONT", Prim_put_work_in_front, 1, 1, 0)
  121. {
  122.   PRIMITIVE_HEADER (1);
  123.   {
  124.     SCHEME_OBJECT queue = (Get_Fixed_Obj_Slot (The_Work_Queue));
  125.     if (queue == EMPTY_LIST)
  126.       {
  127.     queue = (cons (EMPTY_LIST, EMPTY_LIST));
  128.     Set_Fixed_Obj_Slot (The_Work_Queue, queue);
  129.       }
  130.     {
  131.       SCHEME_OBJECT queue_head = (PAIR_CAR (queue));
  132.       SCHEME_OBJECT new_entry = (cons ((ARG_REF (1)), queue_head));
  133.       SET_PAIR_CAR (queue, new_entry);
  134.       if (queue_head == EMPTY_LIST)
  135.     SET_PAIR_CDR (queue, new_entry);
  136.     }
  137.   }
  138.   PRIMITIVE_RETURN (UNSPECIFIC);
  139. }
  140.  
  141. DEFINE_PRIMITIVE ("DRAIN-WORK-QUEUE!", Prim_drain_queue, 0, 0, 0)
  142. {
  143.   PRIMITIVE_HEADER (0);
  144.   {
  145.     SCHEME_OBJECT queue = (Get_Fixed_Obj_Slot (The_Work_Queue));
  146.     Set_Fixed_Obj_Slot (The_Work_Queue, EMPTY_LIST);
  147.     PRIMITIVE_RETURN ((queue != EMPTY_LIST) ? (PAIR_CAR (queue)) : EMPTY_LIST);
  148.   }
  149. }
  150.  
  151. DEFINE_PRIMITIVE ("PEEK-AT-WORK-QUEUE", Prim_peek_queue, 0, 0, 0)
  152. {
  153.   PRIMITIVE_HEADER (0);
  154.   {
  155.     fast SCHEME_OBJECT queue = (Get_Fixed_Obj_Slot (The_Work_Queue));
  156.     if (queue == EMPTY_LIST)
  157.       PRIMITIVE_RETURN (EMPTY_LIST);
  158.     /* Reverse the queue and return it.
  159.        (Why is it being reversed? -- cph) */
  160.     {
  161.       fast SCHEME_OBJECT this_pair = (PAIR_CAR (queue));
  162.       fast SCHEME_OBJECT result = EMPTY_LIST;
  163.       while (this_pair != EMPTY_LIST)
  164.     {
  165.       result = (cons ((PAIR_CAR (this_pair)), result));
  166.       this_pair = (PAIR_CDR (this_pair));
  167.     }
  168.       PRIMITIVE_RETURN (result);
  169.     }
  170.   }
  171. }
  172.  
  173. DEFINE_PRIMITIVE ("GET-WORK", Prim_get_work, 1, 1, 0)
  174. {
  175.   PRIMITIVE_HEADER (1);
  176.   {
  177.     SCHEME_OBJECT thunk = (ARG_REF (1));
  178.     /* This gets this primitive's code which is in the expression register. */
  179.     SCHEME_OBJECT primitive = (Regs [REGBLOCK_PRIMITIVE]);
  180.     SCHEME_OBJECT queue = (Get_Fixed_Obj_Slot (The_Work_Queue));
  181.     SCHEME_OBJECT queue_head =
  182.       ((queue == EMPTY_LIST) ? EMPTY_LIST : (PAIR_CAR (queue)));
  183.     if (queue_head == EMPTY_LIST)
  184.       {
  185.     if (thunk == SHARP_F)
  186.       {
  187.         fprintf (stderr,
  188.              "\nNo work available, but some has been requested!\n");
  189.         Microcode_Termination (TERM_EXIT);
  190.       }
  191.     PRIMITIVE_CANONICALIZE_CONTEXT ();
  192.     POP_PRIMITIVE_FRAME (1);
  193.       Will_Push ((2 * (STACK_ENV_EXTRA_SLOTS + 1)) + 1 + CONTINUATION_SIZE);
  194.     /* When the thunk returns, call the primitive again.
  195.        If there's still no work, we lose. */
  196.     STACK_PUSH (SHARP_F);
  197.     STACK_PUSH (primitive);
  198.     STACK_PUSH (STACK_FRAME_HEADER + 1);
  199.     Store_Expression (SHARP_F);
  200.     Store_Return (RC_INTERNAL_APPLY);
  201.     Save_Cont ();
  202.     /* Invoke the thunk. */
  203.     STACK_PUSH (thunk);
  204.     STACK_PUSH (STACK_FRAME_HEADER);
  205.       Pushed ();
  206.     PRIMITIVE_ABORT (PRIM_APPLY);
  207.       }
  208.     {
  209.       SCHEME_OBJECT result = (PAIR_CAR (queue_head));
  210.       queue_head = (PAIR_CDR (queue_head));
  211.       SET_PAIR_CAR (queue, queue_head);
  212.       if (queue_head == EMPTY_LIST)
  213.     SET_PAIR_CDR (queue, EMPTY_LIST);
  214.       PRIMITIVE_RETURN (result);
  215.     }
  216.   }
  217. }
  218.  
  219. DEFINE_PRIMITIVE ("AWAIT-SYNCHRONY", Prim_await_sync, 1, 1, 0)
  220. {
  221.   PRIMITIVE_HEADER (1);
  222.   CHECK_ARG (1, PAIR_P);
  223.   if (! (FIXNUM_P (PAIR_CDR (ARG_REF (1)))))
  224.     error_bad_range_arg (1);
  225.   PRIMITIVE_RETURN (UNSPECIFIC);
  226. }
  227.  
  228. DEFINE_PRIMITIVE ("N-INTERPRETERS", Prim_n_interps, 0, 0, 0)
  229. {
  230.   PRIMITIVE_HEADER (0);
  231.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (1));
  232. }
  233.  
  234. DEFINE_PRIMITIVE ("MY-PROCESSOR-NUMBER", Prim_my_proc, 0, 0, 0)
  235. {
  236.   PRIMITIVE_HEADER (0);
  237.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (0));
  238. }
  239.  
  240. DEFINE_PRIMITIVE ("MY-INTERPRETER-NUMBER", Prim_my_interp_number, 0, 0, 0)
  241. {
  242.   PRIMITIVE_HEADER (0);
  243.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (0));
  244. }
  245.  
  246. DEFINE_PRIMITIVE ("ZERO-ZONES", Prim_zero_zones, 0, 0, 0)
  247. {
  248.   long i;
  249.   PRIMITIVE_HEADER (0);
  250. #ifdef METERING
  251.   for (i=0; i < Max_Meters; i++)
  252.   {
  253.     Time_Meters[i] = 0;
  254.   }
  255.  
  256.   Old_Time = (OS_process_clock ());
  257. #endif
  258.   PRIMITIVE_RETURN (UNSPECIFIC);
  259. }
  260.  
  261. /* These are really used by GC on a true parallel machine */
  262.  
  263. DEFINE_PRIMITIVE ("GC-NEEDED?", Prim_gc_needed, 0, 0, 0)
  264. {
  265.   PRIMITIVE_HEADER (0);
  266.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((Free + GC_Space_Needed) >= MemTop));
  267. }
  268.  
  269. DEFINE_PRIMITIVE ("SLAVE-GC-BEFORE-SYNC", Prim_slave_before, 0, 0, 0)
  270. {
  271.   PRIMITIVE_HEADER (0);
  272.   PRIMITIVE_RETURN (UNSPECIFIC);
  273. }
  274.  
  275. DEFINE_PRIMITIVE ("SLAVE-GC-AFTER-SYNC", Prim_slave_after, 0, 0, 0)
  276. {
  277.   PRIMITIVE_HEADER (0);
  278.   PRIMITIVE_RETURN (UNSPECIFIC);
  279. }
  280.  
  281. DEFINE_PRIMITIVE ("MASTER-GC-BEFORE-SYNC", Prim_master_before, 0, 0, 0)
  282. {
  283.   PRIMITIVE_HEADER (0);
  284.   PRIMITIVE_RETURN (UNSPECIFIC);
  285. }
  286.  
  287. DEFINE_PRIMITIVE ("MASTER-GC-LOOP", Prim_master_gc, 1, 1, 0)
  288. {
  289.   static SCHEME_OBJECT gc_prim = SHARP_F;
  290.   extern SCHEME_OBJECT make_primitive ();
  291.   PRIMITIVE_HEADER (1);
  292.   PRIMITIVE_CANONICALIZE_CONTEXT();
  293.   /* This primitive caches the Scheme object for the garbage collector
  294.      primitive so that it does not have to perform a potentially
  295.      expensive search each time. */
  296.   if (gc_prim == SHARP_F)
  297.     gc_prim = (make_primitive ("GARBAGE-COLLECT"));
  298.   {
  299.     SCHEME_OBJECT argument = (ARG_REF (1));
  300.     POP_PRIMITIVE_FRAME (1);
  301.   Will_Push (STACK_ENV_EXTRA_SLOTS + 2);
  302.     STACK_PUSH (argument);
  303.     STACK_PUSH (gc_prim);
  304.     STACK_PUSH (STACK_FRAME_HEADER + 1);
  305.   Pushed ();
  306.     PRIMITIVE_ABORT (PRIM_APPLY);
  307.   }
  308. }
  309.